Categories
Quasar

Developing Vue Apps with the Quasar Library — Touch and Click Events

Spread the love

Quasar is a popular Vue UI library for developing good looking Vue apps.

In this article, we’ll take a look at how to create Vue apps with the Quasar UI library.

Touch Hold Directive

We can add the v-touch-hold directive to run code when we click and hold the mouse button on an element.

For instance, we can write:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <div class="q-pa-md">  
        <q-card  
          v-touch-hold.mouse="handleHold"  
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"  
        >  
          <div v-if="info" class="custom-info">  
            <pre>{{ info }}</pre>  
          </div>  
          <div v-else class="text-center">  
            Click me  
          </div>  
        </q-card>  
      </div>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {  
          info: null  
        },  
        methods: {  
          handleHold({ evt, ...info }) {  
            this.info = info;  
          }  
        }  
      });  
    </script>  
  </body>  
</html>

We get the data from the click event listener, which includes whether a tap or click is done.

Also, we get the position of the mouse click or tap and the duration.

The listener method runs 600ms after a click or tap is done by default.

We can change it with the argument:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <div class="q-pa-md">  
        <q-card  
          v-touch-hold:2000.mouse="handleHold"  
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"  
        >  
          <div v-if="info" class="custom-info">  
            <pre>{{ info }}</pre>  
          </div>  
          <div v-else class="text-center">  
            Click me  
          </div>  
        </q-card>  
      </div>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {  
          info: null  
        },  
        methods: {  
          handleHold({ evt, ...info }) {  
            this.info = info;  
          }  
        }  
      });  
    </script>  
  </body>  
</html>

We changed the listen to run 2000ms after a click or tap.

Also, we can change the sensitivity with some arguments:

<!DOCTYPE html>  
<html>  
  <head>  
    <link  
      href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons"  
      rel="stylesheet"  
      type="text/css"  
    />  
    <link  
      href="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.min.css"  
      rel="stylesheet"  
      type="text/css"  
    />  
  </head>  
  <body class="body--dark">  
    <script src="https://cdn.jsdelivr.net/npm/vue@^2.0.0/dist/vue.min.js"></script>  
    <script src="https://cdn.jsdelivr.net/npm/quasar@1.12.13/dist/quasar.umd.min.js"></script>  
    <div id="q-app">  
      <div class="q-pa-md">  
        <q-card  
          v-touch-hold:2000:12:15.mouse="handleHold"  
          class="custom-area cursor-pointer bg-primary text-white shadow-2 relative-position row flex-center"  
        >  
          <div v-if="info" class="custom-info">  
            <pre>{{ info }}</pre>  
          </div>  
          <div v-else class="text-center">  
            Click me  
          </div>  
        </q-card>  
      </div>  
    </div>  
    <script>  
      new Vue({  
        el: "#q-app",  
        data: {  
          info: null  
        },  
        methods: {  
          handleHold({ evt, ...info }) {  
            this.info = info;  
          }  
        }  
      });  
    </script>  
  </body>  
</html>

We change the sensitivity to 12px for touch events and 15px for clicks.

Conclusion

We can add listeners for various mouse and touch events with the directives provided by Quasar.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *